$(document).ready(function() {
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	var operatorIds = parent.$("#operatorIds").val(); //运营商ids
	var placeIds = parent.$("#placeIds").val(); //场景ids
	var zoneIds = parent.$("#zoneIds").val(); //广告位ids
	var osIds = parent.$("#osIds").val(); //系统ids
	var timepicker1 = parent.$("#timepicker1").val();
	$("#timepicker1").val(timepicker1);
	var timepicker2 = parent.$("#timepicker2").val();
	$("#timepicker2").val(timepicker2);

	$(".operator-container .data").show();
	$.ajax({
		async: false,
		type: "POST",
		url: "queryOperatorName",
		success: function(jsondata) {
			if (jsondata != null) {
				var operatorIdArr = [];
				if (operatorIds != "") {

					operatorIdArr = operatorIds.split(",");
				} else {
					$(".operator-container .selectedList .selectedTooltip").show();
				}
				for (var i = 0; i < jsondata.length; i++) {
					var html = '';
					html += '<div class="single_div">';
					html += '<input type="checkbox" id="' + jsondata[i].companyName + '_' + jsondata[i].operatorId + '" value="' + jsondata[i].operatorId + '" title="' + jsondata[i].companyName + '">';
					html += '<label class="single_div_span" for="' + jsondata[i].companyName + '_' + jsondata[i].operatorId + '" title="' + jsondata[i].companyName + '">' + jsondata[i].companyName + '</label>';
					html += '</div>';
					if (operatorIdArr != null && $.inArray(jsondata[i].operatorId + "", operatorIdArr) != -1) {
						$(".operator-container .selectedList").append(html);
					} else {
						$(".operator-container .selectableList").append(html);
					}
				}
			} else {
				$(".operator-container .selectableList .selectedTooltip").show();
			}
		}
	});

	if (placeIds != "") {
		$(".place-container .inputCheckbox input[type='checkbox']").attr("checked", false);
	}

	if (zoneIds != "") {
		$(".zone-container .inputCheckbox input[type='checkbox']").attr("checked", false);
	}

	if (osIds != "") {
		$(".os-container .inputCheckbox input[type='checkbox']").attr("checked", false);
	}

	$(".container .selectableList").each(function() {
		if ($(this).find(".single_div").length > 0) {
			$(this).find(".selectableTooltip").hide();
		}
	});
	$(".container .selectedList").each(function() {
		if ($(this).find(".single_div").length > 0) {
			$(this).find(".selectedTooltip").hide();
		}
	});

	//全选
	$(".allSelect").click(function() {
		$(this).parents(".container").find(".selectableList input[type='checkbox']").attr("checked", true);
	});
	//取消全选
	$(".allNotSelect").click(function() {
		$(this).parents(".container").find(".selectableList input[type='checkbox']").attr("checked", false);
	});
	//选中
	$(".selectBtn .btnSelected").click(function() {
		var obj = $(this).parents(".container");
		var selectedObj = obj.find(".selectableList input[type='checkbox']:checked");
		if (selectedObj.length > 0) {
			var html = '';
			selectedObj.each(function() {
				html += '<div class="single_div">';
				html += '<input type="checkbox" id="' + $(this).attr("title") + '_' + $(this).val() + '" value="' + $(this).val() + '" title="' + $(this).attr("title") + '">';
				html += '<label class="single_div_span" for="' + $(this).attr("title") + '_' + $(this).val() + '" title="' + $(this).attr("title") + '">' + $(this).attr("title") + '</label>';
				html += '</div>';
				$(this).parent(".single_div").remove();
			});
			obj.find(".selectedList .selectedTooltip").hide();
			obj.find(".selectedList").append(html);
			if (obj.find(".selectableList .single_div").length == 0) {
				obj.find(".selectableList .selectableTooltip").show();
			}
			obj.nextAll(".container").find(".selectableList .single_div").remove();
			obj.nextAll(".container").find(".selectedList .single_div").remove();
			if (obj.nextAll(".container").length == 4) {
				operatorIds = "";
				placeIds = "";
				zoneIds = "";
				osIds = "";
			} else if (obj.nextAll(".container").length == 3) {
				placeIds = "";
				zoneIds = "";
				osIds = "";
			} else if (obj.nextAll(".container").length == 2) {
				zoneIds = "";
				osIds = "";
			} else if (obj.nextAll(".container").length == 1) {
				osIds = "";
			}
		}
	});
	//取消
	$(".selectBtn .btnNotSelected").click(function() {
		var obj = $(this).parents(".container");
		var selectedObj = obj.find(".selectedList input[type='checkbox']:checked");
		if (selectedObj.length > 0) {
			var html = '';
			selectedObj.each(function() {
				html += '<div class="single_div">';
				html += '<input type="checkbox" id="' + $(this).attr("title") + '_' + $(this).val() + '" value="' + $(this).val() + '" title="' + $(this).attr("title") + '">';
				html += '<label class="single_div_span" for="' + $(this).attr("title") + '_' + $(this).val() + '" title="' + $(this).attr("title") + '">' + $(this).attr("title") + '</label>';
				html += '</div>';
				$(this).parent(".single_div").remove();
			});
			obj.find(".selectableList .selectableTooltip").hide();
			obj.find(".selectableList").append(html);
			if (obj.find(".selectedList .single_div").length == 0) {
				obj.find(".selectedList .selectedTooltip").show();
			}
			obj.nextAll(".container").find(".selectableList .single_div").remove();
			obj.nextAll(".container").find(".selectedList .single_div").remove();
			if (obj.nextAll(".container").length == 4) {
				operatorIds = "";
				placeIds = "";
				zoneIds = "";
				osIds = "";
			} else if (obj.nextAll(".container").length == 3) {
				placeIds = "";
				zoneIds = "";
				osIds = "";
			} else if (obj.nextAll(".container").length == 2) {
				zoneIds = "";
				osIds = "";
			} else if (obj.nextAll(".container").length == 1) {
				osIds = "";
			}
		}
	});

	$(".inputCheckbox input[type='checkbox'], .inputCheckbox label").click(function() {
		var parentObj = $(this).parents(".container");
		if (parentObj.find(".data-icon").hasClass("down")) {
			parentObj.find(".selectableList .single_div").remove();
			parentObj.find(".selectedList .single_div").remove();
			if (parentObj.hasClass("operator-container")) {
				operatorIds = "";
			} else if (parentObj.hasClass("place-container")) {
				placeIds = "";
			} else if (parentObj.hasClass("zone-container")) {
				zoneIds = "";
			} else if (parentObj.hasClass("os-container")) {
				osIds = "";
			}
		} else {

		}
	});

	$(".data-icon").click(function() {
		var parentObj = $(this).parents(".container");
		if ($(this).hasClass("up")) {
			parentObj.find(".data").hide();
			parentObj.find(".data-icon").addClass("down");
			parentObj.find(".data-icon").removeClass("up");
			parentObj.find(".data-icon").text("显示");
		} else if ($(this).hasClass("down")) {
			if (!parentObj.hasClass("dsp-container")) {
				var dspObj = $(".dsp-container .selectedList input[type='checkbox']");
				if (dspObj.length > 0) {
					dspagentId = dspObj.map(function(i) {
						return $(this).val();
					}).toArray().join(',');
				}
			}
			if (parentObj.hasClass("operator-container")) {
				var operatorObj = $(".operator-container .selectedList input[type='checkbox']");
				if (operatorObj.length > 0) {
					operatorIds = operatorObj.map(function(i) {
						return $(this).val();
					}).toArray().join(',');
					getDimensionData("queryOperatorName", "", "", "", parentObj.find(".selectableList"), "place");
				}
			} else if (parentObj.hasClass("place-container")) {
				if (parentObj.find(".inputCheckbox input[type='checkbox']").attr("checked")) {
					bootbox.alert("你已选择不展现该报表，如需查看请去掉‘报表不展现’前面的√，再进行操作！");
					return false;
				} else {
					var operatorObj = $(".operator-container .selectedList input[type='checkbox']");
					if (operatorObj.length > 0) {
						operatorIds = operatorObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
					} else {
					}
				}
			} else if (parentObj.hasClass("zone-container")) {
				if (parentObj.find(".inputCheckbox input[type='checkbox']").attr("checked")) {
					bootbox.alert("你已选择不展现该报表，如需查看请去掉‘报表不展现’前面的√，再进行操作！");
					return false;
				} else {
					var operatorObj = $(".operator-container .selectedList input[type='checkbox']");
					var placeObj = $(".place-container .selectedList input[type='checkbox']");
					if (placeObj.length > 0 && operatorObj.length > 0) {
						operatorIds = operatorObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						placeIds = placeObj.map(function(j) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("queryZoneName", operatorIds, placeIds, "", parentObj.find(".selectableList"), "zone");
					} else if (placeObj.length > 0) {
						placeIds = placeObj.map(function(j) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("queryZoneName", "", placeIds, "", parentObj.find(".selectableList"), "zone");
					} else if (operatorObj.length > 0) {
						operatorIds = operatorObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("queryZoneName", operatorIds, "", "", parentObj.find(".selectableList"), "zone");
					}
					getDimensionData("queryZoneName", "", "", "", parentObj.find(".selectableList"), "zone");
				}
			} else if (parentObj.hasClass("os-container")) {
				if (parentObj.find(".inputCheckbox input[type='checkbox']").attr("checked")) {
					bootbox.alert("你已选择不展现该报表，如需查看请去掉‘报表不展现’前面的√，再进行操作！");
					return false;
				} else {
					var operatorObj = $(".operator-container .selectedList input[type='checkbox']");
					var zoneObj = $(".zone-container .selectedList input[type='checkbox']");
					var placeObj = $(".place-container .selectedList input[type='checkbox']");
					if (zoneObj.length > 0 && operatorObj.length > 0 && placeObj.length > 0) {
						zoneIds = zoneObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						operatorIds = operatorObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						placeIds = placeObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("osName", operatorIds, placeIds, zoneIds, parentObj.find(".selectableList"), "os");
					} else if (zoneObj.length > 0 && operatorObj.length > 0) {
						operatorIds = operatorObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						zoneIds = zoneObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("osName", operatorIds, "", zoneIds, parentObj.find(".selectableList"), "os");
					} else if (operatorObj.length > 0 && placeObj.length > 0) {
						placeIds = placeObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						operatorIds = operatorObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("osName", operatorIds, placeIds, "", parentObj.find(".selectableList"), "os");
					} else if (placeObj.length > 0 && zoneObj.length > 0) {
						placeIds = placeObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						zoneIds = zoneObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("osName", "", placeIds, zoneIds, parentObj.find(".selectableList"), "os");
					} else if (operatorObj.length > 0) {
						operatorIds = operatorObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("osName", operatorIds, "", "", parentObj.find(".selectableList"), "os");
					} else if (placeObj.length > 0) {
						placeIds = placeObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("osName", "", placeIds, "", parentObj.find(".selectableList"), "os");
					} else if (zoneObj.length > 0) {
						zoneIds = zoneObj.map(function(i) {
							return $(this).val();
						}).toArray().join(',');
						getDimensionData("osName", "", "", zoneIds, parentObj.find(".selectableList"), "os");
					}
					getDimensionData("osName", "", "", "", parentObj.find(".selectableList"), "os");
				}
			}
			$(".container .data").hide();
			$(".container .data-icon").text("显示");
			$(".container .data-icon").removeClass("up");
			$(".container .data-icon").addClass("down");
			parentObj.find(".data").show();
			parentObj.find(".data-icon").addClass("up");
			parentObj.find(".data-icon").removeClass("down");
			parentObj.find(".data-icon").text("隐藏");
		}

	});

	$(".btn-save").click(function() {
		var operatorObj = $(".operator-container .selectedList input[type='checkbox']");
		if (operatorObj.length > 0) {
			operatorIds = operatorObj.map(function(i) {
				return $(this).val();
			}).toArray().join(',');
		} else {
			operatorIds = "";
		}
		var placeObj = $(".place-container .selectedList input[type='checkbox']");
		if (placeObj.length > 0) {
			placeIds = placeObj.map(function(i) {
				return $(this).val();
			}).toArray().join(',');
		} else {
			placeIds = "";
		}
		var zoneObj = $(".zone-container .selectedList input[type='checkbox']");
		if (zoneObj.length > 0) {
			zoneIds = zoneObj.map(function(i) {
				return $(this).val();
			}).toArray().join(',');
		} else {
			zoneIds = "";
		}
		var osObj = $(".os-container .selectedList input[type='checkbox']");
		if (osObj.length > 0) {
			osIds = osObj.map(function(i) {
				return $(this).val();
			}).toArray().join(',');
		} else {
			osIds = "";
		}
		var timepicker1 = $("#timepicker1").val();
		var timepicker2 = $("#timepicker2").val();
		parent.$("#operatorIds").val(operatorIds);
		parent.$("#placeIds").val(placeIds);
		parent.$("#zoneIds").val(zoneIds);
		parent.$("#osIds").val(osIds);
		parent.$("#timepicker1hidden").val(timepicker1);
		parent.$("#timepicker2hidden").val(timepicker2);
		parent.$("#myform").submit();
		parent.layer.close(index); //执行关闭
	});

	$(".btn-cancel").click(function() {
		parent.layer.close(index); //执行关闭
	});

	function getDimensionData(url, operatorIdsData, placeIdsData, zoneIdsData, obj, tab) {
		if (obj.find(".single_div").length == 0 && obj.parents(".container").find(".selectedList .single_div").length == 0) {
			$.ajax({
				async: false,
				type: "POST",
				url: url,
				data: {
					operatorId: operatorIdsData,
					placeId: placeIdsData,
					zoneId: zoneIdsData
				},
				success: function(jsondata) {
					if (jsondata != null) {
						var idsArr = [];
						if (tab == "operator") {
							if (operatorIds != "") {
								idsArr = operatorIds.split(",");
							}
							for (var i = 0; i < jsondata.length; i++) {
								var html = '';
								html += '<div class="single_div">';
								html += '<input type="checkbox" id="' + jsondata[i].companyName + '_' + jsondata[i].operatorId + '" value="' + jsondata[i].operatorId + '" title="' + jsondata[i].companyName + '">';
								html += '<label class="single_div_span" for="' + jsondata[i].companyName + '_' + jsondata[i].operatorId + '" title="' + jsondata[i].companyName + '">' + jsondata[i].companyName + '</label>';
								html += '</div>';
								if (idsArr != null && $.inArray(jsondata[i].operatorId + "", idsArr) != -1) {
									obj.parents(".container").find(".selectedList").append(html);
								} else {
									obj.append(html);
								}
							}
						} else if (tab == "place") {
							if (placeIds != "") {
								idsArr = placeIds.split(",");
							}
							for (var i = 0; i < jsondata.length; i++) {
								var html = '';
								html += '<div class="single_div">';
								html += '<input type="checkbox" id="' + jsondata[i].placeName + '_' + jsondata[i].placeId + '" value="' + jsondata[i].placeId + '" title="' + jsondata[i].placeName + '">';
								html += '<label class="single_div_span" for="' + jsondata[i].placeName + '_' + jsondata[i].placeId + '" title="' + jsondata[i].placeName + '">' + jsondata[i].placeName + '</label>';
								html += '</div>';
								if (idsArr != null && $.inArray(jsondata[i].placeId + "", idsArr) != -1) {
									obj.parents(".container").find(".selectedList").append(html);
								} else {
									obj.append(html);
								}
							}
						} else if (tab == "zone") {
							if (zoneIds != "") {
								idsArr = zoneIds.split(",");
							}
							for (var i = 0; i < jsondata.length; i++) {
								var html = '';
								html += '<div class="single_div">';
								html += '<input type="checkbox" id="' + jsondata[i].zoneName + '_' + jsondata[i].zoneId + '" value="' + jsondata[i].zoneId + '" title="' + jsondata[i].zoneName + '">';
								html += '<label class="single_div_span" for="' + jsondata[i].zoneName + '_' + jsondata[i].zoneId + '" title="' + jsondata[i].zoneName + '">' + jsondata[i].zoneName + '</label>';
								html += '</div>';
								if (idsArr != null && $.inArray(jsondata[i].zoneId + "", idsArr) != -1) {
									obj.parents(".container").find(".selectedList").append(html);
								} else {
									obj.append(html);
								}
							}
						} else if (tab == "os") {
							if (osIds != "") {
								idsArr = osIds.split(",");
							}
							for (var i = 0; i < jsondata.length; i++) {
								var html = '';
								html += '<div class="single_div">';
								html += '<input type="checkbox" id="' + jsondata[i].os + '_' + jsondata[i].os + '" value="' + jsondata[i].os + '" title="' + jsondata[i].name + '">';
								html += '<label class="single_div_span" for="' + jsondata[i].os + '_' + jsondata[i].os + '" title="' + jsondata[i].name + '">' + jsondata[i].name + '</label>';
								html += '</div>';
								if (idsArr != null && $.inArray(jsondata[i].os + "", idsArr) != -1) {
									obj.parents(".container").find(".selectedList").append(html);
								} else {
									obj.append(html);
								}
							}
						}
					}
				}
			});
		}
	}

});